package com.xxxy.dongyexuan.action;

import com.alibaba.fastjson.JSON;
import com.xxxy.dongyexuan.mapper.IUserMapper;
import com.xxxy.dongyexuan.po.PageBean;
import com.xxxy.dongyexuan.po.User;
import com.xxxy.dongyexuan.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user") // 指定请求的路径前缀
public class UserAction {
    @Autowired
    public IUserMapper mapper;

    /**
     * 用户登录验证
     *
     * @param
     * @return ResponseEntity 返回登录结果
     */
    @PostMapping("/login")
//    public ResponseEntity<?> login(@RequestBody User user) {
    public User login(String uname, String upassword, String biaozhi, String yanzhengma) throws Exception {
        // 假设我们的IUserMapper有findUserByUsernameAndPassword方法
//        System.out.println(uname + " " + upassword + " " + biaozhi + " " + yanzhengma);
//        User authenticatedUser = mapper.findUserByUsernameAndPassword(uname, upassword);
        //调用工具类中的方法获取图片的验证码,赋值给fyanzhengma
        String fyanzhengma = TuAction.codemap.get(biaozhi);
        //输出服务端验证码
        System.out.println("服务器端验证码:" + fyanzhengma);
        //如果客户端输出的验证码等于服务端生成的验证码
        if (yanzhengma.equalsIgnoreCase(fyanzhengma)) {
            //输出验证码验证成功
            System.out.println("验证码验证成功");
            //接下来验证用户名和密码是否一直
            //从数据库中查询用户名、密码给user1

        } else {
            //如股票验证码不相同，输出验证失败
            System.out.println("验证失败");
            //抛出异常
            throw new Exception();
        }
        User user1 = mapper.findUserByUsernameAndPassword(uname, upassword);
        //输出这个user1
        System.out.println(user1);
        //如果user1不为空
        if (user1 != null) {
            return user1;
            // return JWTUtil.createJWT(str);
        } else {
            //否则抛出异常
            throw new Exception();
        }
    }

    @PostMapping("/register")
//    public ResponseEntity<?> login(@RequestBody User user) {
    public String register(String uname, String upassword, String biaozhi, String yanzhengma) throws Exception {
        // 假设我们的IUserMapper有findUserByUsernameAndPassword方法
//        System.out.println(uname + " " + upassword + " " + biaozhi + " " + yanzhengma);
//        User authenticatedUser = mapper.findUserByUsernameAndPassword(uname, upassword);
        //调用工具类中的方法获取图片的验证码,赋值给fyanzhengma
        String fyanzhengma = TuAction.codemap.get(biaozhi);
        //输出服务端验证码
        System.out.println("服务器端验证码:" + fyanzhengma);
        //如果客户端输出的验证码等于服务端生成的验证码
        if (yanzhengma.equals(fyanzhengma)) {
            //输出验证码验证成功
            System.out.println("验证码验证成功");
            //接下来验证用户名和密码是否一直
            //从数据库中查询用户名、密码给user1

        } else {
            //如股票验证码不相同，输出验证失败
            System.out.println("验证失败");
            //抛出异常
            throw new Exception();
        }
        User user = new User();
        user.setUname(uname);
        user.setUrealname(uname);
        user.setUpassword(upassword);
        int save = mapper.save(user);
        //输出这个user1
        //如果user1不为空
        if (save > 0) {
            return "注册成功";
        } else {
            //否则抛出异常
            throw new Exception();
        }
    }

    static class LoginResponse {
        private boolean success;
        private String message;

        public LoginResponse(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }
    }


    //查询所有
    @GetMapping("/user")
    public List<User> findAll() {
        List<User> list = this.mapper.findAll();
        return list;
    }

    @GetMapping("/vipUser/{pagenow}/{pagesize}")
    public PageBean findVipUserAll(@PathVariable("pagenow") int pagenow,
                                     @PathVariable("pagesize") int pagesize) {
        //开始条数
        int from = (pagenow - 1) * pagesize;
        //总条数
        int total = this.mapper.getVipCount();
        //计算总页数
        int pages = (total - 1) / pagesize + 1;
        List data = this.mapper.findVipUserAll(from, pagesize);
        PageBean pageBean = new PageBean();
        pageBean.setPages(pages);
        pageBean.setPagenow(pagenow);
        pageBean.setPagesize(pagesize);
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }

    //根据员工号查询
    @GetMapping("/user/{uid}")
    public User findById(@PathVariable("uid") int uid) {
        User user = this.mapper.findById(uid);
        return user;
    }

    //增加
    @PostMapping("/user")
    public void save(@RequestBody Map<String, String> requestParams) throws Exception {
        String uname = requestParams.get("uname");
        String upassword = requestParams.get("upassword");
        int uid = Integer.parseInt(requestParams.get("uid"));
        User user = new User();
        user.setUid(uid);
        user.setUname(uname);
        user.setUpassword(upassword);
        mapper.save(user);
    }

    //修改
    @PutMapping("/user")
    public int update(User user) {
        int result = this.mapper.merge(user);
        return result;
    }

    //删除
    @DeleteMapping("/user/{uid}")
    public int delete(@PathVariable("uid") int uid) {
        int result = this.mapper.delete(uid);
        return result;
    }
}
